﻿using System;
using System.Collections.Generic;

using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BaseSet.Data;
using BaseSet.Logic;
using BaseSet.Entity;
using BaseSet.Framework;
using BaseSet.Entity.Common;
using BaseSet.Entity.Common.Text;
using BaseSet;
using System.Data;
namespace WebSite.Admin.SystemSet
{
    public partial class AdminAdd : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                BindRole();
            }
        }

        #region 添加
        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            string roleid = ddlRole.SelectedValue;
            string username = txtUserName.Text.Trim();
            string nickname = txtNickName.Text.Trim();
            string password = txtPassword.Text.Trim();

            if (roleid == "-1")
            {
                Tools.Alert("请选择权限！");
                return;
            }
            if (string.IsNullOrEmpty(username))
            {
                Tools.Alert("请输入用户名！");
                return;
            }
            if (string.IsNullOrEmpty(nickname))
            {
                Tools.Alert("请输入昵称！");
                return;
            }
            if (string.IsNullOrEmpty(password))
            {
                Tools.Alert("请输入密码！");
                return;
            }

            Users uinfo = new Users();
            uinfo.RoleID = roleid;
            uinfo.RoleType = 0;
            uinfo.UserName = username;
            uinfo.NickName = nickname;
            uinfo.Password = Encrypt.Encode(password);//密码进行加密

            UsersHelper uhelper = new UsersHelper();
            uhelper.AddUsers(uinfo);

            Tools.AlertAndRedirect("添加后台账号成功", "AdminList.aspx");
        }
        #endregion

        #region 加载权限
        private void BindRole()
        {
            RoleHelper rhelper = new RoleHelper();
            Criteria c = new Criteria();
            c.Add(CriteriaType.Equals, "RoleType", 0);
            List<Role> list = rhelper.QueryRoleBySearch(c, null);

            ddlRole.DataSource = list;
            ddlRole.DataTextField = "RoleName";
            ddlRole.DataValueField = "ID";
            ddlRole.DataBind();

            ListItem li = new ListItem();
            li.Value = "-1";
            li.Text = "-请选择-";
            ddlRole.Items.Insert(0, li);
        }
        #endregion
    }
}